package com.literature.threeGroup.controller;

import com.literature.threeGroup.pojo.Log;
import com.literature.threeGroup.pojo.Menu;
import com.literature.threeGroup.pojo.User;
import com.literature.threeGroup.service.MenuService;
import com.literature.threeGroup.service.UserService;
import com.literature.threeGroup.util.JwtUtil;
import com.literature.threeGroup.util.PageResult;
import com.literature.threeGroup.util.Result;
import com.literature.threeGroup.util.StatusCode;
import com.literature.threeGroup.vo.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author VULCAN
 * @create 2020-01-07 14:31
 * @desc
 **/
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;
    /**
     * @name  用户登录
     * @Author VULCAN
     * @Date 2020/1/9 13:47
     */
    @RequestMapping(value = "userlogin",method = RequestMethod.POST)
    public Result userlogin(@RequestBody User user ) throws UnknownHostException {
        User findall = userService.userlogin(user);
        if(findall != null){
            //更改登录时间
            findall.setUserLoginDate(new Date());
            userService.updatelogindate(findall);
            String tokenId = jwtUtil.createJWT(findall.getUserId(),findall.getUserName(), "tokenId");
            redisTemplate.delete("tokenId");
            redisTemplate.delete("loginId");
            redisTemplate.opsForValue().set("tokenId",tokenId,60, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set("loginId",findall.getUserId(),60, TimeUnit.MINUTES);
            return new Result(true , StatusCode.OK,"登录成功",findall);
        }
        return new Result(false , StatusCode.ERROR,"登录失败");
    }
    /**
      * @name  退出 删除token
     * @Author VULCAN
     * @Date 2020/1/31 16:57
     */
    @RequestMapping(value = "error",method = RequestMethod.GET)
    public void error(){
        redisTemplate.delete("tokenId");
        redisTemplate.delete("loginId");
    }
    /**
      * @name  用户管理
     * @Author VULCAN
     * @Date 2020/1/9 14:04
     */
    @RequestMapping(value = "findall",method = RequestMethod.GET)
    public List<User> findall(){
        return userService.findall();
    }
    /**
      * @name  vo数据
     * @Author VULCAN
     * @Date 2020/1/9 19:27
     */
    @RequestMapping(value = "findByAll",method = RequestMethod.GET)
    public List<UserRole> findByAll(){
        return userService.findByAll();
    }
    /**
      * @name  根据id查询
     * @Author VULCAN
     * @Date 2020/1/9 19:27
     */
    @RequestMapping(value = "findById/{userId}",method = RequestMethod.GET)
    public User findById(@PathVariable("userId") String userId){
        return userService.findbyid(userId);
    }
    /**
      * @name  修改数据
     * @Author VULCAN
     * @Date 2020/1/9 19:27
     */
    @RequestMapping(value = "userupdate/{userId}",method = RequestMethod.PUT)
    public void userupdate(@PathVariable("userId") String userId,@RequestBody User user) throws UnknownHostException {
        user.setUserId(userId);
         userService.userupdate(user);
    }
    /**
     * @name  添加数据
     * @Author VULCAN
     * @Date 2020/1/9 19:27
     */
    @RequestMapping(value = "userinsert",method = RequestMethod.POST)
    public void userinsert(@RequestBody User user) throws UnknownHostException {

        userService.userinsert(user);
    }
    /**
     * @name  修改密码
     * @Author VULCAN
     * @Date 2020/1/9 19:27
     */
    @RequestMapping(value = "userupdatepwd/{userId}",method = RequestMethod.PUT)
    public void userupdatepwd(@PathVariable("userId") String userId,@RequestBody User user) throws UnknownHostException {
        user.setUserId(userId);
        userService.userupdatepwd(user);
    }
    /**
     * @name  分页 模糊
     * @Author VULCAN
     * @Date 2020/1/10 19:59
     */
    @RequestMapping(value = "findByAllname/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Result findByAllname(@RequestBody UserRole userRole, @PathVariable("pageNum") int pageNum , @PathVariable("pageSize") int pageSize){
        PageResult findallpage = userService.findByAllname(userRole,pageNum, pageSize);
        return new Result(true, StatusCode.OK,"查询成功",findallpage) ;
    }
    /**
     * @name  批量删除
     * @Author VULCAN
     * @Date 2020/1/17 9:47
     */
    @RequestMapping(value = "userbulkdel/{id}",method = RequestMethod.GET)
    public String userbulkdel(@PathVariable String id) throws UnknownHostException {
        String[] ids = id.split(",");
        System.out.println(ids);
        return userService.userbulkdel(ids);
    }
    /**
     * @name  usercount
     * @Author VULCAN
     * @Date 2020/1/28 14:25
     */
    @RequestMapping(value = "usercount",method = RequestMethod.GET)
    public List usercount(){
        return userService.usercount();
    }
}
